// pages/login/bind/index.js
const app = getApp()
import {
  checkPhone, toast
} from '../../../utils/util'
import {
  login
} from '../../../login/index'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    codeTime: '获取验证码',
    phone: '',
    code: '',
    username: '',
    timer: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initUserName()
  },
  initUserName() {
    this.setData({
      username: wx.getStorageSync('userInfo').nickname
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearTimeout(this.data.timer)
    this.setData({
      timer: null
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(this.data.timer)
    this.setData({
      timer: null
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  bindCommit(x) {
    const flag = this.checkContent()
    flag ? this.commit() : ''
  },
  // 提交前检查
  checkContent() {
    const {
      phone,
      code,
      username
    } = this.data
    if (username.trim() === '') {
      toast('用户名不能为空', 1000)
      return false
    }
    if (!checkPhone(phone)) {
      toast('手机号码格式错误', 1000)
      return false
    }
    if (code.trim() === '') {
      toast('验证码不能为空', 1000)
      return false
    }
    return true
  },
  bindUser(e) {
    this.setData({
      username: e.detail.value
    })
  },
  bindPhone(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  bindCode(e) {
    this.setData({
      code: e.detail.value
    })
  },
  bindGetCode() {
    const phone = this.data.phone
    if (!checkPhone(phone)) {
      wx.showToast({
        title: '手机号码格式不正确',
        icon: 'none',
        duration: 1000
      })
      return false
    }
    this.checkTime()
  },
  checkTime() {
    const GET_CODE = '获取验证码'
    const date = +new Date()
    const minute = 1 * 60 * 1000
    const beforeTime = wx.getStorageSync('GET_CODE_TIME')
    const reduceTime = Math.ceil(60 - (date - beforeTime) / 1000)
    console.log(beforeTime)
    if (beforeTime) {
      if (date - Number(beforeTime) > minute) {
        wx.setStorageSync('GET_CODE_TIME', date)
        this.setData({
          codeTime: 60
        })
        this.countDown()
        this.getCode()
      } else {
        wx.showToast({
          title: `请${reduceTime}秒后再试`,
          icon: 'none',
          duration: 1000
        })
      }
    } else {
      this.setData({
        codeTime: 60
      })
      wx.setStorageSync('GET_CODE_TIME', date)
      this.countDown()
      this.getCode()
    }
  },
  countDown() {
    let codeTime = this.data.codeTime
    if (this.data.timer) clearTimeout(this.data.timer)
    if (codeTime > 0) {
      this.setData({
        codeTime: --codeTime
      })
      this.data.timer = setTimeout(() => {
        this.countDown()
      }, 1000)
    } else {
      this.setData({
        codeTime: '获取验证码'
      })
    }
  },
  getCode() {
    const phone = this.data.phone
    app.$api.login.getSendMsg({
      phone
    }).then((res) => {
      res = res.data
      if (res.success) {
        return
      }
    }).catch((e) => {
      toast('请稍后再试', 1000)
    })
  },
  commit() {
    const url = '../../index/index'
    const refereeid = wx.getStorageSync('refereeid') || ''
    const data = {
      phone: this.data.phone,
      code: this.data.code,
      webcatid: wx.getStorageSync('userInfo').webcatid,
      refereeid: refereeid,
      nickname: wx.getStorageSync('userInfo').nickname,
      headurl: wx.getStorageSync('userInfo').headurl
    }
    app.$api.login.regist({
      ...data
    }).then((res) => {
      console.log(res)
      res = res.data
      if (res.success) {
        toast('注册成功', 1000)
        wx.removeStorageSync('sessionKey')
        wx.reLaunch({
          url: '/pages/index/index'
        })
      }
    }).catch((e) => {
      toast('请稍后再试', 1000)
    })
  }
})